package com.deuwise.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.deuwise.common.utils.BitStatesUtils;
import com.deuwise.common.utils.ReflectUtils;
import com.deuwise.system.entity.ConfigDo;
import com.deuwise.system.entity.PassWordConfig;
import com.deuwise.system.entity.from.PassConfigForm;
import com.deuwise.system.mapper.ConfigDoMapper;
import com.deuwise.system.service.ConfigDoService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Copyright
 *
 * @author chenwenchao
 * @date 2019/3/19
 * Description:
 * 配置实现类
 */
@Service
public class ConfigDoServiceImpl extends ServiceImpl<ConfigDoMapper, ConfigDo> implements ConfigDoService {



    @Override
    public PassConfigForm getPassWordConfig() {
        Wrapper<ConfigDo> configDoWrapper=new EntityWrapper<>();
        configDoWrapper.eq("code","passWordFrom");
        ConfigDo configDo=new PassWordConfig();
        configDo=this.selectOne(configDoWrapper);
        if(configDo!=null) {
            Wrapper<ConfigDo> configDoWrapper1 = new EntityWrapper<>();
            configDoWrapper1.eq("code", "overDue");
            ConfigDo configDo1 = this.selectOne(configDoWrapper1);

            Wrapper<ConfigDo> configDoWrapper2 = new EntityWrapper<>();
            configDoWrapper2.eq("code", "passwordLen");
            ConfigDo configDo2 = this.selectOne(configDoWrapper2);

            Wrapper<ConfigDo> configDoWrapper3 = new EntityWrapper<>();
            configDoWrapper3.eq("code", "sysLogoUrl");
            ConfigDo configDo3 = this.selectOne(configDoWrapper3);

            PassWordConfig pwdConfig =new PassWordConfig();
            try {
                ReflectUtils.fatherToChild(configDo,pwdConfig);
            } catch (Exception e) {
                e.printStackTrace();
            }
            PassConfigForm passConfigForm = new PassConfigForm();
            passConfigForm.setId(pwdConfig.getId());
            //设置最新密码过期时间
            passConfigForm.setUpdateTime(configDo1.getUpdateTime());
            passConfigForm.setCheck(pwdConfig.getIsCheckPassWord());
            if (pwdConfig.getIsCheckPassWord()) {
                passConfigForm.setIncludeLowerCase(pwdConfig.getIncludeLowerCase());
                passConfigForm.setIncludeNum(pwdConfig.getIncludeNum());
                passConfigForm.setIncludeUpperCase(pwdConfig.getIncludeUpperCase());
                passConfigForm.setIncludeSpecial(pwdConfig.getIncludeSpecial());
            }
            passConfigForm.setOverDue(pwdConfig.getOverDue());
            passConfigForm.setOverDueDay(configDo1.getValue());
            passConfigForm.setPassWordLen(configDo2.getValue());
            passConfigForm.setSysLogoUrl(configDo3.getValue());
            return passConfigForm;
        }
        return  null;
    }

    @Override
    public Integer updatePassWordConfig(PassConfigForm passConfigForm) {
        if(passConfigForm.getId()!=null) {
            PassWordConfig pwdConfig=new PassWordConfig();
            ConfigDo configDo = this.selectById(passConfigForm.getId());
            try {
                ReflectUtils.fatherToChild(configDo,pwdConfig);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (passConfigForm.isCheck()) {
                //设置密码项
                pwdConfig.addState(BitStatesUtils.USE_PASSWORD_CHECK);
                if (passConfigForm.isIncludeNum()) {
                    pwdConfig.addState(BitStatesUtils.INCLUDE_NUM);
                }else {
                    pwdConfig.removeState(BitStatesUtils.INCLUDE_NUM);
                }
                if (passConfigForm.isIncludeLowerCase()) {
                    pwdConfig.addState(BitStatesUtils.INCLUDE_LOWERCASE);
                }else {
                    pwdConfig.removeState(BitStatesUtils.INCLUDE_LOWERCASE);
                }
                if (passConfigForm.isIncludeUpperCase()) {
                    pwdConfig.addState(BitStatesUtils.INCLUDE_UPPERCASE);
                }else {
                    pwdConfig.removeState(BitStatesUtils.INCLUDE_UPPERCASE);
                }
                if (passConfigForm.isIncludeSpecial()) {
                    pwdConfig.addState(BitStatesUtils.INCLUDE_SPECIAL);
                }else {
                    pwdConfig.removeState(BitStatesUtils.INCLUDE_SPECIAL);
                }
            } else {
                pwdConfig.removeState(BitStatesUtils.USE_PASSWORD_CHECK);
                pwdConfig.removeState(BitStatesUtils.INCLUDE_NUM);
                pwdConfig.removeState(BitStatesUtils.INCLUDE_LOWERCASE);
                pwdConfig.removeState(BitStatesUtils.INCLUDE_UPPERCASE);
                pwdConfig.removeState(BitStatesUtils.INCLUDE_SPECIAL);
            }
            configDo = (ConfigDo) pwdConfig;

            //获取密码有效时间
            Wrapper<ConfigDo> configDoWrapper1 = new EntityWrapper<>();
            configDoWrapper1.eq("code", "overDue");
            ConfigDo configDo1 = this.selectOne(configDoWrapper1);
            if (passConfigForm.isOverDue()) {
                pwdConfig.addState(BitStatesUtils.OVER_DUE);

            } else {
                pwdConfig.removeState(BitStatesUtils.OVER_DUE);
                configDo1.setValue(null);
            }

            if (passConfigForm.isOverDue() && !passConfigForm.getOverDueDay().equals(configDo1.getValue())) {
                configDo1.setValue(passConfigForm.getOverDueDay());
                configDo1.setUpdateTime(new Date());
            }
            //获取密码起始长度
            Wrapper<ConfigDo> configDoWrapper2 = new EntityWrapper<>();
            configDoWrapper2.eq("code", "passwordLen");
            ConfigDo configDo2 = this.selectOne(configDoWrapper2);
            configDo2.setValue(passConfigForm.getPassWordLen());

            // 系统logoUrl配置sysLogoUrl
            Wrapper<ConfigDo> configDoWrapper3 = new EntityWrapper<>();
            configDoWrapper3.eq("code", "sysLogoUrl");
            ConfigDo configDo3 = this.selectOne(configDoWrapper3);
            configDo3.setValue(passConfigForm.getSysLogoUrl());

            this.updateById(configDo);
            this.updateById(configDo1);
            this.updateById(configDo2);
            this.updateById(configDo3);
        }else{
            return  0;
        }
        return  1;
    }
}
